﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SistemaVarejo.DAO;
using SistemaVarejo.Model;

namespace SistemaVarejo.View
{
    public partial class frmConsultaCliente : Form
    {

        public int id { set; get; }
        public frm_venda frmVenda;

        public frmConsultaCliente()
        {
            InitializeComponent();
        }
        public frmConsultaCliente(Form form)
        {
            InitializeComponent();
            frmVenda = (frm_venda)form;
        }

        private void frmConsultaCliente_Load(object sender, EventArgs e)
        {
            List<Cliente> c = ClienteDAO.BuscaTodosOsClientes();
            PreencheGrid(c);
        }

        public List<Cliente> PreencheGrid(List<Cliente> c) {
            gridConsultaClientes.Rows.Clear();

            foreach(Cliente x in c){
                gridConsultaClientes.Rows.Add(x.Id, x.cpf, x.nome);
            }
            return c.ToList();

        }

        private void txtNome_TextChanged(object sender, EventArgs e)
        {
            Cliente c = new Cliente();
            c.nome = txtNome.Text;
            procuraNome(c);
        }

        private void procuraNome(Cliente c) {
            List<Cliente> listC = ClienteDAO.FiltraNome(c);
            PreencheGrid(listC);
        }

        private void procuraCpf(Cliente c) {
            List<Cliente> listC = ClienteDAO.FiltraCpf(c);
            PreencheGrid(listC);
        }

        private void txtCpf_TextChanged(object sender, EventArgs e)
        {
            Cliente c = new Cliente();
            c.cpf = txtCpf.Text;
            procuraCpf(c);
        }


        private void gridConsultaClientes_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Venda v = new Venda();
            string nome;
            nome = (string)(sender as DataGridView).Rows[e.RowIndex].Cells[2].Value;
            v.cliente = nome;
            frmVenda.preencheCliente(v);
            this.Close();

        }
    }
}
